package com.qiaofang.applet.rest;

import com.qiaofang.applet.common.dto.DataRequest;
import com.qiaofang.applet.common.dto.DataResponse;
import com.qiaofang.applet.service.member.service.MemberService;
import com.qiaofang.applet.service.member.service.dto.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Map;

/**
 * @author daiyuxaing
 */
@Slf4j
@RestController
@RequestMapping("/api/web/member")
@Api(tags = "会员：会员模块")
public class MemberController {

    @Autowired
    MemberService memberService;

    /**
     * 发送验证码
     * @param request
     * @return
     */
    @PostMapping("/sendCaptcha")
    @ApiOperation(value = "发送验证码", notes = "发送验证码")
    public DataResponse<Object> sendCaptcha(@RequestBody @Valid DataRequest<SendCaptchaDto> request) {
        if (memberService.sendCaptcha(request.getParam())) {
            return DataResponse.ok();
        }
        return DataResponse.fail("发送验证码失败");
    }

    @PostMapping("/getPicCaptcha")
    @ApiOperation(value = "获取图片验证码", notes = "获取图片验证码")
    public DataResponse<Map<String, Object>> getPicCaptcha(){
        return DataResponse.ok(memberService.getPicCaptcha());
    }

    /**
     * 登录
     * @param request
     * @return
     */
    @PostMapping("/phoneLogin")
    @ApiOperation(value = "手机号登录", notes = "手机号登录")
    public DataResponse<MemberLoginDto> phoneLogin(@RequestBody @Valid DataRequest<LoginDto> request) {
        return DataResponse.ok(memberService.login(request.getParam()));
    }

    /**
     * wxCode换取用户信息
     * @param request
     * @return
     */
    @PostMapping("/memberSession")
    @ApiOperation(value = "wxCode换取用户信息", notes = "wxCode换取用户信息")
    public DataResponse<MemberLoginDto> memberSession(@RequestBody @Valid DataRequest<String> request) {
        return DataResponse.ok(memberService.memberSession(request.getParam()));
    }

    @PostMapping("/decryptPhone")
    @ApiOperation(value = "解密手机号码", notes = "解密手机号码")
    public DataResponse<String> decryptPhone(@RequestBody DataRequest<DecryptPhoneReqDto> request){
        return DataResponse.ok(memberService.decryptPhone(request.getParam()));
    }

    /**
     * 微信注册
     * @param request
     * @return
     */
    @PostMapping("/regist")
    @ApiOperation(value = "微信注册", notes = "微信注册")
    public DataResponse<MemberLoginDto> regist(@RequestBody @Valid DataRequest<MemberRegisterReqDto> request) {
        return DataResponse.ok(memberService.wxRegist(request.getParam()));
    }

    @PostMapping("/updateHeadPortraitAndName")
    @ApiOperation(value = "通过用户授权更新头像和昵称信息", notes = "通过用户授权更新头像和昵称信息")
    public DataResponse<Object> updateHeadPortraitAndName(@RequestBody @Valid DataRequest<HeadPortraitAndNameDto> request) {
        memberService.updateHeadPortraitAndName(request.getParam());
        return DataResponse.ok();
    }

}
